package fr.umlv.projet.android.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.util.Base64;

public class StringUtils {

	public static String inputStreamToString(InputStream is) throws IOException {
		String line = "";
		StringBuilder total = new StringBuilder();
		BufferedReader rd = new BufferedReader(new InputStreamReader(is));
		while ((line = rd.readLine()) != null) {
			total.append(line);
		}
		return total.toString();
	}

	public static String inputStreamToStringOfByte(InputStream is)
			throws IOException {
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		int nRead;
		byte[] data = new byte[16384];
		while ((nRead = is.read(data, 0, data.length)) != -1) {
			buffer.write(data, 0, nRead);
		}
		buffer.flush();
		return new String(Base64.encode(buffer.toByteArray(), Base64.DEFAULT));
	}

	public static InputStream stringToInputStream(String s) throws IOException {
		InputStream is = new ByteArrayInputStream(s.getBytes("UTF-8"));
		return is;
	}

}
